package com.example.demo.controller;

import com.example.demo.entity.Sort;
import com.example.demo.mapper.SortMapper;
import com.example.demo.response.RegisterResponse;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class SortController {
    @Resource
    SortMapper SortMapper;

    @GetMapping("/getSort")
    public List<Sort> getSort(){
        return SortMapper.findAll();
    }

    @PostMapping("/addSort")
    public RegisterResponse register(@RequestBody Sort Sort){
        Sort existingSort = SortMapper.findBySortname(Sort.getSortName());
        if (existingSort != null) {
            // 用户名已存在，返回错误信息
            return new RegisterResponse(false,"分类已存在");
        }
        SortMapper.add(Sort);
        return new RegisterResponse(true,"添加分类成功");
    }
    @PostMapping("/updateSort")
    public RegisterResponse updateSort(@RequestBody Sort Sort) {

        SortMapper.update(Sort);
        return new RegisterResponse(true, "更新分类成功");
    }
    @PostMapping("/deleteSort")
    public RegisterResponse deleteSort(@RequestBody Sort Sort) {
        SortMapper.deleteById(Sort);
        return new RegisterResponse(true, "删除分类成功");
    }
}
